"""
非线性回归
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from linear_regression import LinearRegression

# 得到训练和测试数据
data = pd.read_csv('../data/non-linear-regression-x-y.csv')

x = data['x'].values.reshape((data.shape[0], 1))
y = data['y'].values.reshape((data.shape[0], 1))

plt.plot(x, y)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Linear Regression')
plt.show()

num_iterations = 50000
learning_rate = 0.01
polynomial_degree = 15
sinusoid_degree = 15
normalize_data = True

linear_regression = LinearRegression(x, y, polynomial_degree, sinusoid_degree, normalize_data)
theta, cost_history = linear_regression.train(learning_rate, num_iterations)

print('开始时的损失:', cost_history[0])
print('训练后的损失:', cost_history[-1])

# 训练梯度下降曲线图
plt.plot(range(num_iterations), cost_history)
plt.xlabel('Iter')
plt.ylabel('Cost')
plt.title('GDP')
plt.show()

predictions_num = 100
x_predictions = np.linspace(x.min(), x.max(), predictions_num).reshape(predictions_num, 1)
y_predictions = linear_regression.predict(x_predictions)

plt.scatter(x, y, label='Train data')
plt.plot(x_predictions, y_predictions, 'r', label='Predictions')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('First')
plt.legend()
plt.show()

